<?php
class CouponorderAction extends CommonAction{
    
	public function index(){
		
		if(isMobile()){
			header('Location:'.C('SITE_URL').'/m'.U("Couponorder/index"));
		}
		
		$province = M('Region')->where ( array('pid'=>1) )->select ();
		$this->assign('province',$province);
		
		//首页通知
		$this->assign('indexdesc',$this->indexdesc);
		
		//是否闭站
		$this->assign('sitestatus',$this->sitestatus);
		
		$title = "诺亚阳湖大闸蟹提货官网";
		
		$this->seo($title, C(SITE_KEYWORDS), C(SITE_DESCRIPTION));
		
		$this->display();
	}

    public function insert(){
    	
    	$data['status'] = 0;
    	$data['info'] = '';
    	
    	//闭站处理
    	if($this->sitestatus == 1){
    		$data['info'] = $this->indexdesc;
    		$this->ajaxReturn($data);exit;
    	}
    	
    	$number = trim(I('number'));
    	$password = trim(I('password'));
    	$name = trim(I('name'));
    	$mobile = trim(I('mobile'));
    	$province = trim(I('province'));
    	$city = trim(I('city'));
    	$town = trim(I('town'));
    	$address = trim(I('address'));
		
    	if(!$number || !$password){
    		$data['info'] = '礼卡卡号或者密码不正确！';
    		$this->ajaxReturn($data);exit;
    	}
    	
    	if(!$name){
    		$data['info'] = '请填写姓名';
    		$this->ajaxReturn($data);exit;
    	}
    	
    	if(!$mobile){
    		$data['info'] = '请填写手机号码';
    		$this->ajaxReturn($data);exit;
    	}
    	
    	if(!$province){
    		$data['info'] = '请填写省份';
    		$this->ajaxReturn($data);exit;
    	}

    	if(!$city){
    		$data['info'] = '请填写城市';
    		$this->ajaxReturn($data);exit;
    	}
    	
    	if(!$town){
    		$data['info'] = '请填写区域';
    		$this->ajaxReturn($data);exit;
    	}
    	
    	if(!$address){
    		$data['info'] = '请填写详细地址';
    		$this->ajaxReturn($data);exit;
    	}
    	
    	//券信息
    	$coupon_info = M('coupon')->where("number='".$number."' and is_used=0")->find();

    	if(!$coupon_info){
    		$data['info'] = '礼卡错误或者已经被使用！';
    		$this->ajaxReturn($data);exit;
    	}
    	
    	if($coupon_info['status'] <=0 ){
    		$data['info'] = '对不起，该礼卡已被禁用！';
    		$this->ajaxReturn($data);
    	}
    	
    	if($coupon_info['password'] != md5(md5($password).$coupon_info['salt'])){
    		$data['info'] = '礼卡卡号或者密码错误！';
    		$this->ajaxReturn($data);exit;
    	}
    	
    	//生成订单
    	$model = D('Couponorder');
    	if (false === $model->create()) {
    		//$data['info'] = $model->getError();
    		$data['info'] = '订单提交失败，请联系我们的客服：400-166-1058';
    		$this->ajaxReturn($data);exit;
    	}
    	
    	$model->order_id = $orderid = createOrderId();
    	$model->coupon_id = $coupon_info['id'];
    	$model->type_id = $coupon_info['type_id'];
    	$list = $model->add();
    	if($list){
    		//使用券
    		$coupon_info = M('coupon')->where("number='".$number."'")->setField('is_used',1);
    		$coupon_info = M('coupon')->where("number='".$number."'")->setField('used_time',time());
    		$data['status'] = 1;
    		$data['info'] = '恭喜您，提货成功！<br>您的订单号是：'.$orderid.'<br>我们将在48小时之内为您发货！';
    		$this->ajaxReturn($data);exit;
    	}else{
    		//失败提示
    		$data['info'] = '订单提交失败，请联系我们的客服：400-166-1058';
    		$this->ajaxReturn($data);exit;
    	}
    } 
	
    public function verification(){
    	
    	$data['status'] = 0;
    	$data['info'] = '';
    	
    	//闭站处理
    	if($this->sitestatus == 1){
    		$data['info'] = $this->indexdesc;
    		$this->ajaxReturn($data);exit;
    	}
    	
    	//v($_POST);
    	$number = trim(I('number'));
    	$password = trim(I('password'));
		
    	if($number && $password){
			$coupon_info = M('coupon')->where("number='".$number."'")->find();
			if($coupon_info){
				
				if($coupon_info['status'] <=0 ){
					$data['info'] = '对不起，该礼卡已被禁用！';
					$this->ajaxReturn($data);
				}
				
				if($coupon_info['password'] != md5(md5($password).$coupon_info['salt'])){
					$data['info'] = '礼卡卡号或者密码错误！';
					$this->ajaxReturn($data);exit;
				}

				//已经提货 自动进入信息查询页面
				if($coupon_info['is_used'] == 1){
					
					$couponorder_info = M('Couponorder')->where("coupon_id=".$coupon_info['id'])->find();
					
					$data['status'] = 2;
					$data['order_id'] = $couponorder_info['order_id'];
					$data['coupon_name'] = getCouponType($couponorder_info['coupon_id']);
					$data['name'] = $couponorder_info['name'];
					$data['mobile'] = $couponorder_info['mobile'];
					$data['province'] = getRegion($couponorder_info['province']);
					$data['city'] = getRegion($couponorder_info['city']);
					$data['town'] = getRegion($couponorder_info['town']);
					$data['address'] = $couponorder_info['address'];
					$data['remark'] = $couponorder_info['remark'];
					$data['userremark'] = $couponorder_info['userremark'];
					$data['create_time'] = toDate($couponorder_info['create_time']);
					$data['status_name'] = getCouponorderStatus($couponorder_info['status']);
					$data['deliveryno'] = $couponorder_info['deliveryno'];
					$data['deliverytype'] = getDeliverytype($couponorder_info['deliverytype']);
					//v($data);
					$this->ajaxReturn($data);
				}
				
				$compare_password = md5(md5($password).$coupon_info['salt']);
				
				if($compare_password == $coupon_info['password']){
					$data['status'] = 1;
					$data['info'] = '验证成功！';
					$this->ajaxReturn($data);
				}else{
					$data['info'] = '礼卡卡号或者密码不正确！';
					$this->ajaxReturn($data);
				}
			}else{
				$data['info'] = '礼卡卡号或者密码不正确！';
				$this->ajaxReturn($data);
			}
    		
    	}else{
    		$data['info'] = '礼卡卡号或者密码不正确！';
    		$this->ajaxReturn($data);
    	}
    }
    
    public function search(){
    	 
    	$data['status'] = 0;
    	$data['info'] = '';
    	
    	//v($_POST);
    	$number = trim(I('number'));
    	$password = trim(I('password'));
    
    	if($number && $password){
    		$coupon_info = M('coupon')->where("number='".$number."'")->find();
    		if($coupon_info){
    
    			if($coupon_info['status'] <=0 ){
    				$data['info'] = '对不起，该礼卡已被禁用！';
    				$this->ajaxReturn($data);
    			}
    
    			if($coupon_info['password'] != md5(md5($password).$coupon_info['salt'])){
    				$data['info'] = '礼卡卡号或者密码不正确！';
    				$this->ajaxReturn($data);exit;
    			}
    			
    			//已经提货 自动进入信息查询页面
    			if($coupon_info['is_used'] == 1){		
    				$couponorder_info = M('Couponorder')->where("coupon_id=".$coupon_info['id'])->find();
    					
    				$data['status'] = 2;
    				$data['order_id'] = $couponorder_info['order_id'];
    				$data['coupon_name'] = getCouponType($couponorder_info['coupon_id']);
    				$data['name'] = $couponorder_info['name'];
    				$data['mobile'] = $couponorder_info['mobile'];
    				$data['province'] = getRegion($couponorder_info['province']);
    				$data['city'] = getRegion($couponorder_info['city']);
    				$data['town'] = getRegion($couponorder_info['town']);
    				$data['address'] = $couponorder_info['address'];
    				$data['remark'] = $couponorder_info['remark'];
    				$data['userremark'] = $couponorder_info['userremark'];
    				$data['create_time'] = toDate($couponorder_info['create_time']);
    				$data['status_name'] = getCouponorderStatus($couponorder_info['status']);
    				$data['deliveryno'] = $couponorder_info['deliveryno'];
    				$data['deliverytype'] = getDeliverytype($couponorder_info['deliverytype']);

    				$this->ajaxReturn($data);
    			}else{
    				
    				//闭站处理
    				if($this->sitestatus == 1){
    					$data['info'] = $this->indexdesc;
    					$this->ajaxReturn($data);exit;
    				}
    				
    				$data['status'] = 1;
    				$data['info'] = '<font style="font-size:16px;font-weight:bold;">'.getCouponType($coupon_info['id']).'</font> 元大闸蟹礼卡验证成功！<br/>请点击提蟹按钮立即提蟹！';
    				$this->ajaxReturn($data);
    			}
    		}else{
    			$data['info'] = '礼卡卡号或者密码不正确！';
    			$this->ajaxReturn($data);
    		}
    
    	}else{
    		$data['info'] = '礼卡卡号或者密码不正确！';
    		$this->ajaxReturn($data);
    	}
    }

}
?>